This notebook may be copied from docs/ and used as a trainer-tutor. Download_Openslide_Data

toolkit.image_file_to_patches_directory_for_image_level(run_parameters)

Write patches from a Whole Slide Image using parameters:

Requires three lines of code:
1) import the function
2) declare the parameters
3) call the function with the parameters


Also:

view the output

Use parameters template

View the Whole Slide Image


Page top

1) Import the function (and view the doc_string help) - note the required parameter keys

In [1]:
from pychunklbl.toolkit import image_file_to_patches_directory_for_image_level

help(image_file_to_patches_directory_for_image_level)
Help on function image_file_to_patches_directory_for_image_level in module pychunklbl.toolkit:

image_file_to_patches_directory_for_image_level(run_parameters)
    Usage: image_file_to_patches_directory_for_image_level(run_parameters)
    
    Args:
        run_parameters:         (python dict with these keys)
                                wsi_filename:           file name (with valid path)
                                output_dir:             writeable directory for the tfrecord
                                class_label:            label for all images
                                patch_height:           patch size = (patch_width, patch_height)
                                patch_width:            patch size = (patch_width, patch_height)
                                file_ext:               default is '.jpg' ('.png') was also tested
                                thumbnail_divisor:      wsi_image full size divisor to create thumbnail image
                                patch_select_method:    'threshold_rgb2lab' or 'threshold_otsu'
                                threshold:              minimimum sum of thresholded image (default = 0)
                                image_level:            0 is full scale,
    
    Returns:                    None - writes images to output_dir (possibly many)
                                (prints number of images written)


Page top

2) Declare the required parameters as a python dictionary:

Note that the output directory will be created by the function if it does not exist.
In [2]:
run_parameters = {'wsi_filename': '../data/images/CMU-1-Small-Region.svs',
                  'output_dir': '../../mounted_dir/results', 
                  'class_label': 'batch-name', 
                  'patch_height': 224, 
                  'patch_width': 224, 
                  'patch_select_method': 'threshold_rgb2lab',
                  'thumbnail_divisor': 10,
                  'file_ext': '.jpg',
                  'threshold': 0, 
                  'image_level': 0}

#               display the parameters
for k, v in run_parameters.items():
    print('%25s: %s'%(k, v))
             wsi_filename: ../data/images/CMU-1-Small-Region.svs
               output_dir: ../../mounted_dir/results
              class_label: batch-name
             patch_height: 224
              patch_width: 224
      patch_select_method: threshold_rgb2lab
        thumbnail_divisor: 10
                 file_ext: .jpg
                threshold: 0
              image_level: 0


Page top

3) Call the function with the run parameters:

In [3]:
import time

function_start_time = time.time()

image_file_to_patches_directory_for_image_level(run_parameters)

print('function running time: %0.3f'%(time.time() - function_start_time))
77 images written
function running time: 0.511


Page top

View the output for confirmation and understanding the function-parameters:

In [4]:
import os
from PIL import Image

for f_name in os.listdir(run_parameters['output_dir']):
    im_file = os.path.join(run_parameters['output_dir'], f_name)
    if os.path.isfile(im_file) == True:
        print('\n',f_name)
        display(Image.open(im_file))
 CMU-1-Small-Region_1344_448_testLabel.jpg
 CMU-1-Small-Region_1344_1792_testLabel.jpg
 CMU-1-Small-Region_448_896_batch-name.jpg
 CMU-1-Small-Region_1568_1792_testLabel.jpg
 CMU-1-Small-Region_1120_672_batch-name.jpg
 CMU-1-Small-Region_896_224_batch-name.jpg
 CMU-1-Small-Region_1568_448_testLabel.jpg
 CMU-1-Small-Region_0_1120_testLabel.jpg
 CMU-1-Small-Region_896_1120_batch-name.jpg
 CMU-1-Small-Region_1344_2016_batch-name.jpg
 CMU-1-Small-Region_896_1120_testLabel.jpg
 CMU-1-Small-Region_1120_1792_batch-name.jpg
 CMU-1-Small-Region_1568_1120_batch-name.jpg
 CMU-1-Small-Region_1120_1344_testLabel.jpg
 CMU-1-Small-Region_672_1120_batch-name.jpg
 CMU-1-Small-Region_1344_1568_batch-name.jpg
 CMU-1-Small-Region_1344_1344_batch-name.jpg
 CMU-1-Small-Region_896_2688_testLabel.jpg
 CMU-1-Small-Region_1120_2688_batch-name.jpg
 CMU-1-Small-Region_0_0_batch-name.jpg
 CMU-1-Small-Region_672_896_batch-name.jpg
 CMU-1-Small-Region_1568_1344_testLabel.jpg
 CMU-1-Small-Region_1344_1344_testLabel.jpg
 CMU-1-Small-Region_224_896_batch-name.jpg
 CMU-1-Small-Region_896_896_testLabel.jpg
 CMU-1-Small-Region_1792_896_batch-name.jpg
 CMU-1-Small-Region_672_448_batch-name.jpg
 CMU-1-Small-Region_672_0_testLabel.jpg
 CMU-1-Small-Region_672_224_testLabel.jpg
 CMU-1-Small-Region_1120_1792_testLabel.jpg
 CMU-1-Small-Region_1568_672_batch-name.jpg
 CMU-1-Small-Region_1344_672_batch-name.jpg
 CMU-1-Small-Region_1792_1792_batch-name.jpg
 CMU-1-Small-Region_896_2016_testLabel.jpg
 CMU-1-Small-Region_672_896_testLabel.jpg
 CMU-1-Small-Region_1120_448_batch-name.jpg
 CMU-1-Small-Region_1568_1792_batch-name.jpg
 CMU-1-Small-Region_672_1792_batch-name.jpg
 CMU-1-Small-Region_896_2240_testLabel.jpg
 CMU-1-Small-Region_896_1792_batch-name.jpg
 CMU-1-Small-Region_1120_896_batch-name.jpg
 CMU-1-Small-Region_1120_0_testLabel.jpg
 CMU-1-Small-Region_1120_1120_batch-name.jpg
 CMU-1-Small-Region_224_896_testLabel.jpg
 CMU-1-Small-Region_1344_2240_batch-name.jpg
 CMU-1-Small-Region_1792_1792_testLabel.jpg
 CMU-1-Small-Region_1120_448_testLabel.jpg
 CMU-1-Small-Region_896_224_testLabel.jpg
 CMU-1-Small-Region_672_1792_testLabel.jpg
 CMU-1-Small-Region_448_2464_batch-name.jpg
 CMU-1-Small-Region_1344_2464_batch-name.jpg
 CMU-1-Small-Region_896_2464_testLabel.jpg
 CMU-1-Small-Region_1792_1120_batch-name.jpg
 CMU-1-Small-Region_896_0_testLabel.jpg
 CMU-1-Small-Region_0_0_testLabel.jpg
 CMU-1-Small-Region_896_0_batch-name.jpg
 CMU-1-Small-Region_448_2240_batch-name.jpg
 CMU-1-Small-Region_672_2688_batch-name.jpg
 CMU-1-Small-Region_1568_2688_batch-name.jpg
 CMU-1-Small-Region_1344_448_batch-name.jpg
 CMU-1-Small-Region_672_1344_testLabel.jpg
 CMU-1-Small-Region_1568_896_batch-name.jpg
 CMU-1-Small-Region_224_1120_batch-name.jpg
 CMU-1-Small-Region_896_672_testLabel.jpg
 CMU-1-Small-Region_896_1568_testLabel.jpg
 CMU-1-Small-Region_1344_896_batch-name.jpg
 CMU-1-Small-Region_896_2688_batch-name.jpg
 CMU-1-Small-Region_1792_1344_testLabel.jpg
 CMU-1-Small-Region_1568_448_batch-name.jpg
 CMU-1-Small-Region_1120_2464_testLabel.jpg
 CMU-1-Small-Region_448_2688_testLabel.jpg
 CMU-1-Small-Region_672_224_batch-name.jpg
 CMU-1-Small-Region_672_1344_batch-name.jpg
 CMU-1-Small-Region_1568_1344_batch-name.jpg
 CMU-1-Small-Region_896_448_testLabel.jpg
 CMU-1-Small-Region_896_1344_batch-name.jpg
 CMU-1-Small-Region_1792_2240_batch-name.jpg
 CMU-1-Small-Region_1120_224_testLabel.jpg
 CMU-1-Small-Region_224_1120_testLabel.jpg
 CMU-1-Small-Region_0_896_testLabel.jpg
 CMU-1-Small-Region_1344_2240_testLabel.jpg
 CMU-1-Small-Region_1120_2464_batch-name.jpg
 CMU-1-Small-Region_1792_1120_testLabel.jpg
 CMU-1-Small-Region_1568_2240_testLabel.jpg
 CMU-1-Small-Region_1120_0_batch-name.jpg
 CMU-1-Small-Region_1120_1568_testLabel.jpg
 CMU-1-Small-Region_672_1120_testLabel.jpg
 CMU-1-Small-Region_1568_896_testLabel.jpg
 CMU-1-Small-Region_896_448_batch-name.jpg
 CMU-1-Small-Region_1120_2240_testLabel.jpg
 CMU-1-Small-Region_1568_1568_testLabel.jpg
 CMU-1-Small-Region_1120_2240_batch-name.jpg
 CMU-1-Small-Region_1344_1568_testLabel.jpg
 CMU-1-Small-Region_896_896_batch-name.jpg
 CMU-1-Small-Region_0_896_batch-name.jpg
 CMU-1-Small-Region_672_2688_testLabel.jpg
 CMU-1-Small-Region_1120_672_testLabel.jpg
 CMU-1-Small-Region_0_1120_batch-name.jpg
 CMU-1-Small-Region_1344_896_testLabel.jpg
 CMU-1-Small-Region_1568_2016_batch-name.jpg
 CMU-1-Small-Region_672_2016_batch-name.jpg
 CMU-1-Small-Region_1344_2464_testLabel.jpg
 CMU-1-Small-Region_672_2016_testLabel.jpg
 CMU-1-Small-Region_896_1568_batch-name.jpg
 CMU-1-Small-Region_1344_1120_batch-name.jpg
 CMU-1-Small-Region_1792_2016_testLabel.jpg
 CMU-1-Small-Region_896_2016_batch-name.jpg
 CMU-1-Small-Region_1792_2464_batch-name.jpg
 CMU-1-Small-Region_1568_1568_batch-name.jpg
 CMU-1-Small-Region_672_1568_batch-name.jpg
 CMU-1-Small-Region_1568_2464_testLabel.jpg
 CMU-1-Small-Region_1792_2240_testLabel.jpg
 CMU-1-Small-Region_672_2464_batch-name.jpg
 CMU-1-Small-Region_1120_2688_testLabel.jpg
 CMU-1-Small-Region_448_2464_testLabel.jpg
 CMU-1-Small-Region_1344_1120_testLabel.jpg
 CMU-1-Small-Region_1568_2464_batch-name.jpg
 CMU-1-Small-Region_672_2240_testLabel.jpg
 CMU-1-Small-Region_1792_2016_batch-name.jpg
 CMU-1-Small-Region_896_2464_batch-name.jpg
 CMU-1-Small-Region_672_0_batch-name.jpg
 CMU-1-Small-Region_1568_1120_testLabel.jpg
 CMU-1-Small-Region_1344_2688_batch-name.jpg
 CMU-1-Small-Region_896_1792_testLabel.jpg
 CMU-1-Small-Region_1120_1344_batch-name.jpg
 CMU-1-Small-Region_1344_224_testLabel.jpg
 CMU-1-Small-Region_1344_224_batch-name.jpg
 CMU-1-Small-Region_1120_2016_testLabel.jpg
 CMU-1-Small-Region_1344_1792_batch-name.jpg
 CMU-1-Small-Region_1568_2016_testLabel.jpg
 CMU-1-Small-Region_1120_2016_batch-name.jpg
 CMU-1-Small-Region_448_2240_testLabel.jpg
 CMU-1-Small-Region_1792_2464_testLabel.jpg
 CMU-1-Small-Region_1120_1568_batch-name.jpg
 CMU-1-Small-Region_896_672_batch-name.jpg
 CMU-1-Small-Region_672_448_testLabel.jpg
 CMU-1-Small-Region_448_2688_batch-name.jpg
 CMU-1-Small-Region_1344_2016_testLabel.jpg
 CMU-1-Small-Region_1120_224_batch-name.jpg
 CMU-1-Small-Region_672_2464_testLabel.jpg
 CMU-1-Small-Region_1120_1120_testLabel.jpg
 CMU-1-Small-Region_1344_2688_testLabel.jpg
 CMU-1-Small-Region_672_2240_batch-name.jpg
 CMU-1-Small-Region_1568_2240_batch-name.jpg
 CMU-1-Small-Region_1792_896_testLabel.jpg
 CMU-1-Small-Region_1568_672_testLabel.jpg
 CMU-1-Small-Region_672_1568_testLabel.jpg
 CMU-1-Small-Region_448_896_testLabel.jpg
 CMU-1-Small-Region_896_1344_testLabel.jpg
 CMU-1-Small-Region_896_2240_batch-name.jpg
 CMU-1-Small-Region_1792_1344_batch-name.jpg
 CMU-1-Small-Region_1344_672_testLabel.jpg
 CMU-1-Small-Region_1568_2688_testLabel.jpg
 CMU-1-Small-Region_1120_896_testLabel.jpg


Page top

get and modify parameters from template file:

In [5]:
"""
    Import the toolkit function to read the template .yml file.
    Read in the run_parameters and reset the output_dir location.
    
    Note:   "class_label" parameter is set to "test_label_name" 
            and will produce an reddish warning message when changed to "test-label-name"
"""
from pychunklbl.toolkit import get_run_parameters

parameter_data_dir = '../data/run_files'

#               read the template file into a python dict
run_parameters = get_run_parameters(parameter_data_dir, 'wsi_file_to_patches_dir.yml')

#               reset the output_dir location
run_parameters['output_dir'] = '../../run_dir/test_results'

#               display the parameters
for k, v in run_parameters.items():
    print('%25s: %s'%(k, v))
    

#               Create the directory if it does not exist, else - check contents.
import os

if os.path.isdir(run_parameters['output_dir']) == False:
    os.makedirs(run_parameters['output_dir'])
else:
    out_dir_list = os.listdir(run_parameters['output_dir'])
    print('\n\nOutput directory contents before calling the function %i entries'%len(out_dir_list))
                   method: wsi_2_patches_dir
             wsi_filename: ../data/images/CMU-1-Small-Region.svs
               output_dir: ../../run_dir/test_results
              class_label: test_label_name
        thumbnail_divisor: 10
             patch_height: 224
              patch_width: 224
      patch_select_method: threshold_rgb2lab
        rgb2lab_threshold: 80
                 file_ext: .jpg
              image_level: 0
                threshold: 0
    patch_stride_fraction: 1.0
                 offset_x: 0
                 offset_y: 0
            run_directory: ../data/run_files
                 run_file: wsi_file_to_patches_dir.yml


Output directory contents before calling the function 77 entries
In [6]:
"""
    Display the original image in the rich openslide context
"""
import openslide

test_file_name = run_parameters['wsi_filename']
print('Opening Image file: \n\t{}\n'.format(test_file_name))
openslide_obj = openslide.OpenSlide(test_file_name)

print('openslide.OpenSlide(test_file_name) is type:', type(openslide_obj))
print('obj.level_count', openslide_obj.level_count)
print('obj.dimensions', openslide_obj.dimensions)
print('obj.level_dimensions', openslide_obj.level_dimensions)
print('obj.level_downsamples', openslide_obj.level_downsamples)
print('obj.properties are type', type(openslide_obj.properties))

print('\nobj.associated_images', openslide_obj.associated_images, type(openslide_obj.associated_images))

blind_region = openslide_obj.read_region((0, 0), 0, (200,200))
print('\nobj.read_region', openslide_obj.read_region((0, 0), 0, (200,200)))

scldwn = openslide_obj.level_downsamples[-1]
print('\nobj.get_best_level_for_downsample(%0.6f)'%(scldwn), 
      openslide_obj.get_best_level_for_downsample(scldwn))

thumbnail = openslide_obj.associated_images['thumbnail']
openslide_obj.close()

print('Slide thumbnail')
display(thumbnail)
Opening Image file: 
	../data/images/CMU-1-Small-Region.svs

openslide.OpenSlide(test_file_name) is type: <class 'openslide.OpenSlide'>
obj.level_count 1
obj.dimensions (2220, 2967)
obj.level_dimensions ((2220, 2967),)
obj.level_downsamples (1.0,)
obj.properties are type <class 'openslide._PropertyMap'>

obj.associated_images <_AssociatedImageMap {'label': <PIL.Image.Image image mode=RGBA size=387x463 at 0x7F9B88BB89B0>, 'macro': <PIL.Image.Image image mode=RGBA size=1280x431 at 0x7F9B88BB8978>, 'thumbnail': <PIL.Image.Image image mode=RGBA size=574x768 at 0x7F9B88BB85C0>}> <class 'openslide._AssociatedImageMap'>

obj.read_region <PIL.Image.Image image mode=RGBA size=200x200 at 0x7F9B88BB8668>

obj.get_best_level_for_downsample(1.000000) 0
Slide thumbnail
In [ ]: